from xbd.publics.public_cc import *


class Voucher(PublicCc):
    public = PublicCc()
    keyword = "转0001"  # 传凭证字号/凭证id/摘要
    accountBookId = public.get_bookid('组织41账套-测年终结转1')  # 传账套名或账套id
    data = {
        "accountPeriod": '202412',
        "baseAccountBookId": accountBookId,
        "accountBookId": accountBookId,
        "keyword": keyword
    }
    # 当voucher不是凭证id,则根据凭证字号搜索凭证获取id
    if len(keyword) != 19 or not keyword.isdigit():
        url = '/api/core/v1/finVoucher/getFinVoucherPage'
        res = public.post(url, data)['data']['records']
        public.assertNotEqual(res, [], '该账套下查询不到该凭证，请确认查询字段和账套')
        data.update({"finVoucherId": res[0]['finVoucherId']})

    def test_01(self):
        # self.save_voucher()
        self.submit_audit()
        self.audit()
        self.charg_up()

    def test_02(self):
        self.cancel_entry()
        self.cance_audit()
        # self.del_voucher()

    def save_voucher(self):
        '''新增凭证'''
        data = self.data
        detailList = []
        # 凭证分录，amount是金额，subjectCode是科目名或编号，subjectDirection是分录借贷方向，0是借/1是贷
        lists = [
            {'amount': 10, 'subjectCode': '211', 'subjectDirection': 0},
            {'amount': 10, 'subjectCode': '112004002', 'subjectDirection': 1}
        ]
        for dict in lists:
            subject = self.get_subject(
                book=self.accountBookId,
                subject=dict['subjectCode'])
            dict['summary'] = PublicCqjy.now_str  # 摘要
            dict['subjectCode'] = subject['subjectCode']  # 科目编号
            dict['subjectName'] = subject['subjectName']  # 科目名
            dict['subjectGroupName'] = subject['subjectGroupName']  # 科目层级
            dict['baseAccountSubjectId'] = subject['baseAccountSubjectId']  # 账套id
            detailList.append(dict)
        url = '/api/core/v1/finVoucher/saveFinVoucher'
        data.update({
            "voucherWord": randint(0, 3),  # 凭证字
            # 记账日期
            "voucherDateStr": str(datetime.strptime(data['accountPeriod'], '%Y%m'))[:10],
            "detailList": detailList,
            "finVoucherId": None
        })
        self.post(url, data)

    def submit_audit(self):
        '''提交审核'''
        url = '/api/core/v1/finVoucher/submitAudit'
        self.data.update(
            {"sysOrganizationId": self.read_login_data('sysOrganizationId')})
        self.post(url, self.data)

    def audit(self, auditResult=True):
        '''审核，auditResult=True是通过，False是驳回'''
        url = '/api/core/v1/finVoucher/audit'
        self.data.update({"auditResult": auditResult, "auditTime": str(
            datetime.now() + timedelta(seconds=5)), "auditOpinion": '审批意见1'})
        self.post(url, self.data)

    def charg_up(self):
        '''记账'''
        url = '/api/core/v1/finVoucher/chargeUp'
        self.post(url, self.data)

    def cancel_entry(self):
        '''取消记账'''
        url = '/api/core/v1/finVoucher/cancelEntry'
        self.post(url, self.data)

    def cance_audit(self):
        '''取消审核'''
        url = '/api/core/v1/finVoucher/cancelAudit'
        self.data.update({"auditOpinion": "审核意见1"})
        self.post(url, self.data)

    def del_voucher(self):
        '''删除凭证'''
        url = '/api/core/v1/finVoucher/deleteFinVoucher'
        self.data.update({"deleteRedFlush": False})
        self.post(url, self.data)
